"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/7/9 11:01
@Description : 
链家二手房信息    soup
"""
import random
import time

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

for page in range(1, 4):
    html = requests.get(f'https://hf.lianjia.com/ershoufang/pg{page}/',
                        headers={'User-Agent': UserAgent().random}).content.decode('utf-8')

    soup = BeautifulSoup(html, 'lxml')
    divs = soup.select('.house-text')
    for div in divs:
        house = {}
        house['title'] = div.select('.house-title')[0].text
        house['desc'] = div.select('.house-desc')[0].text
        house['total_price'] = div.select('.price-total')[0].text
        house['unit_price'] = div.select('.price-unit')[0].text
        print(house)
    time.sleep(random.randint(1, 3))
    print('-----------------------------------------------------------')

"""
<div class="house-text">
    <div class="house-title">
        <img
            src="https://img.ljcdn.com/beike/haofanglogo/1571901104492.png"
            alt="必看好房"/>
            (.*?)
    </div>
    <div class="house-desc" title="2室2厅/92.99m²/南/滨河苑(长丰)二手房">
         (.*?)
    </div>
    <div class="house-tags"><span class="tag isVr"
                                  style="background-color:rgba(100,142,249,0.15);color:rgb(100,142,249)"
                                  title="VR房源二手房">VR房源</span><span
            class="tag five"
            style="background-color:rgba(242,161,47,0.15);color:rgb(242,161,47)"
            title="满五年二手房">满五年</span><span class="tag haskey"
                                                    style="background-color:rgba(51,190,133,0.15);color:rgb(51,190,133)"
                                                    title="随时看房二手房">随时看房</span>
    </div>
    <p class="house-price">
    <span class="price-total"> (.*?)</span>
    <span  class="price-unit"> (.*?)</span></p></div>
</div>
"""